<h2>Modify Inventory</h2>

<?php   if(isset($_GET['err']) && $_GET['err'] )
			echo "<font id='std_padding'  color='red'>Error: " . $_GET['err'] . "</font>"; 
		else if(isset($_GET['msg'])  && $_GET['msg'])
			echo "<font id='std_padding'  color='#0246FF'>" . $_GET['msg'] . "</font>";
?>

<?php 

if(!session_id()) session_start(); 
$con = mysql_connect($dbserver, $dbuser, $dbpass);
if ($con)
{
	mysql_select_db($db, $con);
	

		$q = fquery($get_inventory_by_media, array($_GET['id']));
		$result = mysql_query($q);
		
		$media_types = mysql_query($all_media_types);
		
		$book_info = fquery($get_book_info, array($_GET['id']));
		$book_info = mysql_query($book_info);
		$book_info = mysql_fetch_array($book_info);
		$book_title = $book_info['title'];
		$book_img = $book_info['cover_image'];
		
		
		
}
else{
	echo "<font color='red'> Error Connecting to DB!</font>";
}
?>

<h3><?php echo $book_title ?></h3>
<div id='std_padding'>
	<div id='book_img_container_small'>
		<img id='book_search_img' src='<?php echo $book_img ?>' align='left'>
	</div>
</div>
<table id='DB_search_results'>
	<tr>
		<th>Inventory ID</th>
		<th>Media ID</th>
		<th>Cost ($)</th>
		<th>Media Type ID</th>		
	</tr>
	<tr>
		<form action='actions/inventory/add_inventory.php' method='POST' >
			<td></td>
			<td><input size='22' type='hidden' name='media_id' value='<?php echo $_GET['id'] ?>'/> <?php echo $_GET['id'] ?></td>
			<td><input size='22' type='text' name='cost'/></td>
			<td> <select name='media_type_id'>
			<?php while($media = mysql_fetch_array($media_types)){
					echo "<option value = '".$media['media_type_id']."'>".$media['name']."</option>";
			} 
			$media_types = mysql_query($all_media_types); ?>
			
			</select></td>
			<td>&nbsp;&nbsp;&nbsp;<input type="submit" value="Add New" /></td>
		</form>
	</tr>
<?php
	while($inventory_item = mysql_fetch_array($result)){
		if(!isset($_POST['inventory_edit']) || $_POST['inventory_edit'] != $inventory_item['inventory_id']){
			echo "<tr>";
			echo "<td>".$inventory_item['inventory_id']."</td>";
			echo "<td>".$inventory_item['media_id']."</td>";
			echo "<td> $".number_format(((double)$inventory_item['cost'])/((double)100), 2)."</td>";
			echo "<td>";
			while($media = mysql_fetch_array($media_types)){
				if($media['media_type_id'] == $inventory_item['media_type_id'])
					echo $media['name'];
			}
			$media_types = mysql_query($all_media_types);
			echo "</td>";
			echo "<td><form action='/manage_inventory.php?action=display&id=". $inventory_item['media_id']. "' method='post'><input type='hidden' name='inventory_edit' value='".$inventory_item['inventory_id']."' />&nbsp;&nbsp;&nbsp;<input type='submit' value='Edit' /></form></td>";
			echo "</tr>";
		}
		else{
			echo "<tr>";
			echo "<form action='/actions/inventory/modify_inventory.php' method='POST' >";
			echo "<td>".$inventory_item['inventory_id']."<input size='16' type='hidden' name='inventory_id' value='".$inventory_item['inventory_id']."'/></td>";
			echo "<td> <input size='16' type='text' name='media_id' value='".$inventory_item['media_id']."'/></td>";
			echo "<td> <input size='16' type='text' name='cost' value='".number_format(((double)$inventory_item['cost'])/((double)100), 2)."'/></td>";
			echo "<td> <select name='media_type_id'>";
			while($media = mysql_fetch_array($media_types)){
				if($media['media_type_id'] != $inventory_item['media_type_id'])
					echo "<option value = '".$media['media_type_id']."'>".$media['name']."</option>";
				else
					echo "<option SELECTED value = '".$media['media_type_id']."'>".$media['name']."</option>";	
			}
			$media_types = mysql_query($all_media_types);
			echo "</select></td>";
			echo "<input size='16' type='hidden' name='prev_media_id' value='".$inventory_item['media_id']."'/>";
			echo "<td>&nbsp;&nbsp;&nbsp;<input type='submit' value='Save' /></form></td>";
			echo "</form></tr>";
		}
	}
	
	mysql_close($con);
	
?>
</table>

